package dk.aiszone.lang.sql;

import dk.aiszone.lang.sql.SQLResult.UnknownIdException;

/**
 * Representation of the <i>Insert</i> SQL statement.
 * 
 * @author Ais
 */
public class SQLInsert extends AbstractSQLObject {

    private SQLFieldList basisFieldList;
    private SQLValueList basisValueList;

    private SQLFieldList currentFieldList;
    private SQLValueList currentValueList;

    private SQLResult prepared = null;
    private final SQLTable table;

    /**
     * Constructor for the <code>SQLInsert</code> object
     * 
     * @param table
     *            The <code>SQLTable</code> to insert data into.
     */
    public SQLInsert(SQLTable table) {
        this.table = table;
    }

    /**
     * Add a <code>SQLAssignableField</code> to the insert statement.<br>
     * 
     * @param field
     *            The <code>SQLAssignableField</code> to add.
     */
    public void addField(SQLAssignableField field) {
        if (currentFieldList == null) {
            currentFieldList = new SQLFieldList(field);
            basisFieldList = currentFieldList;

            currentValueList = new SQLValueList(field);
            basisValueList = currentValueList;
        } else {
            SQLFieldList tmpField = new SQLFieldList(field);
            currentFieldList.setConstraint2(tmpField);
            currentFieldList = tmpField;

            SQLValueList tmpValue = new SQLValueList(field);
            currentValueList.setConstraint2(tmpValue);
            currentValueList = tmpValue;
        }
    }

    /**
     * Set the value of the assignable constraint with the specified id.
     * 
     * @param id
     *            The id of the assignable constraint to assign value to.
     * @param value
     *            The value to assign.
     * @throws UnknownIdException
     *             Thrown if the specified id is unknown.
     */
    public void setValue(String id, Object value) throws UnknownIdException {
        if (prepared == null) {
            prepare();
        }

        prepared.setValue(id, value);
    }

    /**
     * Convert the <code>SQLInsert</code> to a prepared statement <code>String</code>.
     * 
     * @return The prepared statement <code>String</code> representation of the <code>SQLInsert</code> object.
     */
    public String toPreparedStatement() {
        if (prepared == null) {
            prepare();
        }

        return prepared.toPreparedStatement();
    }

    /**
     * Convert the <code>SQLInsert</code> to a SQL <code>String</code>.
     * 
     * @return The <code>String</code> representation of the <code>SQLINsert</code> object.
     */
    public String toSQL() {
        if (prepared == null) {
            prepare();
        }

        return prepared.toSQL();
    }

    private void prepare() {
        prepared = new SQLResult();

        prepared.append("INSERT INTO ");
        prepared.append(table.getTableName());
        prepared.append(" (");
        basisFieldList.prepareSQL(prepared);
        prepared.append(") VALUES (");
        basisValueList.prepareSQL(prepared);
        prepared.append(")");
    }
}